#include "spdlog/spdlog.h"
#include "device.h"
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>

using namespace std;

//开启socket: socket->bind->connect
int Device::socket_bind() 
{
    //对端通过sdp发送过来的需要的rtp传输方式：TCP/RTP/AVP or RTP/AVP
    if (rtp_protocol == "TCP/RTP/AVP") 
    {
        sockfd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
        if(sockfd==-1)
        {
            spdlog::error("create tcp socket failed: {}", sockfd); 
        }
    } 
    else 
    {
        sockfd = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
        if(sockfd==-1)
        {
            spdlog::error("create udp socket failed: {}", sockfd); 
        }
    }

    sockaddr_in bind_address;
    bind_address.sin_family = AF_INET;
    bind_address.sin_port = htons(local_port+1);
    bind_address.sin_addr.s_addr = inet_addr(local_ip.c_str());
    // bind_address.sin_addr.s_addr = htonl(INADDR_ANY);

    auto bind_status = ::bind(sockfd, (struct sockaddr *)&bind_address, sizeof(bind_address));
    if (bind_status) 
    {
        spdlog::error("socket bind failed: {}", bind_status);
        return bind_status;
    }

    sockaddr_in server_address;
    server_address.sin_family = AF_INET;
    server_address.sin_port = htons(rtp_port);
    inet_pton(AF_INET, rtp_ip.c_str(), &server_address.sin_addr);

    auto connect_status = ::connect(sockfd, (struct sockaddr *)&server_address, sizeof(server_address));
    if ( connect_status < 0) 
    {
        spdlog::error("socket connect failed: {}", connect_status);
        return connect_status;
    }

    return 0;
}
//发送数据包
void Device::send_network_packet(const char * data, int length) 
{
    if (rtp_protocol == "TCP/RTP/AVP") 
    {
        ::send(sockfd, data, length, 0);
    }
    else 
    {
        sockaddr_in server_address;
        server_address.sin_family = AF_INET;
        server_address.sin_port = htons(rtp_port);
        inet_pton(AF_INET, rtp_ip.c_str(), &server_address.sin_addr);
        ::sendto(sockfd, data, length, 0, (struct sockaddr *)&server_address, sizeof(server_address));
    }
}